package com.ruoyi.fina.controller;

import java.util.List;

import com.ruoyi.common.utils.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.fina.domain.FinAccount;
import com.ruoyi.fina.service.IFinAccountService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 账户Controller
 * 
 * @author useragent-1
 * @date 2025-05-26
 */
@Controller
@RequestMapping("/fina/account")
public class FinAccountController extends BaseController
{
    private String prefix = "fina/account";

    @Autowired
    private IFinAccountService finAccountService;

    @RequiresPermissions("fina:account:view")
    @GetMapping()
    public String account()
    {
        return prefix + "/account";
    }

    /**
     * 查询账户列表
     */
    @RequiresPermissions("fina:account:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(FinAccount finAccount)
    {
        startPage();
        List<FinAccount> list = finAccountService.selectFinAccountList(finAccount);
        return getDataTable(list);
    }

    /**
     * 导出账户列表
     */
    @RequiresPermissions("fina:account:export")
    @Log(title = "账户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(FinAccount finAccount)
    {
        List<FinAccount> list = finAccountService.selectFinAccountList(finAccount);
        ExcelUtil<FinAccount> util = new ExcelUtil<FinAccount>(FinAccount.class);
        return util.exportExcel(list, "账户数据");
    }

    /**
     * 新增账户
     */
    @RequiresPermissions("fina:account:add")
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        mmap.put("userString", ShiroUtils.getSysUser().toUserString());
        return prefix + "/add";
    }

    /**
     * 新增保存账户
     */
    @RequiresPermissions("fina:account:add")
    @Log(title = "账户", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(FinAccount finAccount)
    {
        finAccount.setUserId(ShiroUtils.getUserId());//设置当前用户ID
        return toAjax(finAccountService.insertFinAccount(finAccount));
    }

    /**
     * 修改账户
     */
    @RequiresPermissions("fina:account:edit")
    @GetMapping("/edit/{accountId}")
    public String edit(@PathVariable("accountId") Long accountId, ModelMap mmap)
    {
        FinAccount finAccount = finAccountService.selectFinAccountByAccountId(accountId);
        mmap.put("userString",ShiroUtils.getSysUser().toUserString());
        mmap.put("finAccount", finAccount);

        return prefix + "/edit";
    }

    /**
     * 修改保存账户
     */
    @RequiresPermissions("fina:account:edit")
    @Log(title = "账户", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(FinAccount finAccount)
    {
        return toAjax(finAccountService.updateFinAccount(finAccount));
    }

    /**
     * 删除账户
     */
    @RequiresPermissions("fina:account:remove")
    @Log(title = "账户", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(finAccountService.deleteFinAccountByAccountIds(ids));
    }
}
